/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools.dialog;

import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerManager;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FrameChangeOpacity extends ToolFrame
{
    private static final String     M_FRAME_TITLE       = "Change opacity layer";
    private static final Dimension  M_FRAME_SIZE        = new Dimension(200, 75);
    
    private LayerManager            m_imageManager;
    private JButton                 m_buttonOK          = new JButton("OK");
    private JButton                 m_buttonCancel      = new JButton("Cancel");
    private JLabel                  m_label             = new JLabel("Opacity : ");
    private JSpinner                m_opacitySpinner    = new JSpinner();
    private SpinnerNumberModel      m_spinnerModel      = new SpinnerNumberModel(1, 0, 1, 0.01);
    private HashMap                 m_initOpacity;

    public FrameChangeOpacity (LayerManager imageManager, JFrame application)
    {
        m_imageManager = imageManager;
        
        m_initOpacity  = new HashMap();

        this.setTitle(M_FRAME_TITLE);
        this.setSize (M_FRAME_SIZE);
        
        this.setLayout(new GridLayout(2, 2));
        this.add(m_label);
        this.add(m_opacitySpinner);
        this.add(m_buttonOK);
        this.add(m_buttonCancel);

        this.initValues();

        m_opacitySpinner.addChangeListener(new ChangeListener()
        {
            @Override
            public void stateChanged(ChangeEvent e)
            {
                updateValues();
            }
        });

        m_buttonCancel.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                close();
                setOldValues();

            }
        });
        
        m_buttonOK.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                updateValues();
                close();
            }
        });

        this.pack();
    }

    private void initValues ()
    {
        float minOpacity = ALayer.M_DEFAULT_OPACITY;

        this.m_initOpacity.clear();

        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            this.m_initOpacity.put(img, img.getM_opacity());
            
            minOpacity = Math.min(minOpacity, img.getM_opacity());
        }

        m_opacitySpinner.setModel(m_spinnerModel);
        
        this.m_spinnerModel.setValue(minOpacity);
    }

    private void updateValues ()
    {
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            img.setM_opacity(this.m_spinnerModel.getNumber().floatValue());
        }
    }

    private void setOldValues ()
    {
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            img.setM_opacity((float) this.m_initOpacity.get(img));
        }
    }

    @Override
    public void close ()
    {
        super.close();
        
        this.setEnabled(true);
        this.dispose();
    }
}
